// pages/goods_list/index.ts
import {
  request
} from '../../request/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 总条数
    total: 0,
    allDataList: [],
    tabs: [{
        id: 0,
        value: "综合",
        goodsList: []
      },
      {
        id: 1,
        value: "销量",
        goodsList: []
      },
      {
        id: 2,
        value: "价格",
        goodsList: []
      }
    ]
  },
  // 接口参数
  QueryParams: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize: 10
  },
  async getGoodsList() {
    let res = await request({
      url: 'goods/search',
      data: this.QueryParams
    })
    var checked = "tabs[0].goodsList"
    this.setData({
      allDataList: res.goods,
      [checked]: res.goods,
      total: res.total
    })
    // 关闭下拉刷新效果
    wx.stopPullDownRefresh()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.QueryParams.cid = options.cid || ''
    this.QueryParams.query = options.query || ''
    this.getGoodsList()

  },
  tabsItemChange(target) {
    console.log(target.detail)
    this.getGoodsList()
    var checked = "tabs[" + target.detail.index + "].goodsList"
    this.setData({
      [checked]: this.data.allDataList
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.QueryParams.pagesize >= this.data.total) {
      wx.showToast({
        title: '已经到底了'
      })
    } else {
      this.QueryParams.pagesize += 10
      this.getGoodsList()
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.QueryParams.pagesize = 10
    this.getGoodsList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },





  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})